﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using ConsolidationTool.Common;
using System.Collections.Generic;
using ConsolidationTool.Common.Entities;
using Microsoft.SharePoint;
using System.Linq;

namespace ConsolidationTool.WebParts.Flash_Input.Flash_Input_TSRGC.TSRGCDisplayForm
{
    public partial class TSRGCDisplayFormUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindParentProduct();
        }
        void BindHeadProduct()
        {
            if (ViewState["RGC"] != null)
            {
                DataTable dt = (DataTable)ViewState["RGC"];
                GridViewHeadProduct.DataSource = dt;
                GridViewHeadProduct.DataBind();

            }
        }
    
        /// <summary>
        /// 邦定历史产品数据
        /// </summary>
        void BindParentProduct()
        {
            SPListItem item = Title.ListItem;
            List<RGCInParentForm> headProduct = new List<RGCInParentForm>();
            List<Region> regions = Common.BusinessService.getRegion().Where(r => r.regionName == item["Region"].ToString()).ToList();
            List<SubRegion> sbregion = Common.BusinessService.getSubRegion().Where(sr => sr.subRegionName == item["SubRegion"].ToString()).ToList();
            Region rg = new Region();
            SubRegion srg = new SubRegion();

            if (regions != null && regions.Count > 0)
                rg = regions[0];
            if (sbregion != null && sbregion.Count > 0)
                srg = sbregion[0];
            headProduct = Common.BusinessService.GetRGCByParentForm(Constants.List_Name_HeadProductTSRGC, rg.regionId.ToString(), srg.subRegionId.ToString(), item["CoCd"].ToString(), item["Year"].ToString());
            if (headProduct != null)
            {
                DataTable dtRGC = new DataTable();
                dtRGC.Columns.Add("ID");
                dtRGC.Columns.Add("Title");
                dtRGC.Columns.Add("Description");
                dtRGC.Columns.Add("Qty", typeof(float));
                dtRGC.Columns.Add("Turnover", typeof(float));
                dtRGC.Columns.Add("Qty_Current", typeof(float));
                dtRGC.Columns.Add("Turnover_Current", typeof(float));
                foreach (RGCInParentForm hp in headProduct)
                {
                    RGC rgcH = new RGC();
                    rgcH = Common.BusinessService.GetRGCByID(hp.ID)[0];
                    DataRow drRGC = dtRGC.NewRow();
                    drRGC["ID"] = hp.ID;
                    drRGC["Title"] = hp.Title;
                    drRGC["Description"] = rgcH.Description;
                    drRGC["Qty"] = hp.Qty;
                    drRGC["Turnover"] = hp.Turnover;
                    drRGC["Qty_Current"] = 0;
                    drRGC["Turnover_Current"] = 0;
                    dtRGC.Rows.Add(drRGC);
                }
                ViewState["RGC"] = dtRGC;
                BindHeadProduct();
            }
            List<Entity> entities = Common.BusinessService.getEntity().Where(e => e.CoCd == item["CoCd"].ToString()).ToList();
            Entity et = new Entity();
            if (entities != null && entities.Count > 0)
                et = entities[0];
          
        }
    }
}
